<?php
/**
 * Created by andrei
 * Date: 10/17/12
 * Time: 11:06 AM
 */
class Page_Fe_Products_Personalization extends Page {
	/**
	 * @var Db_Products_Products
	 */
	protected $tableObject;

	function __construct() {
		self::$TPL->setBodyClass('personalization');
		parent::__construct();
	}

	public function defaultAction() {
		self::$TPL->setBodyTemplate('personalization');
		self::$TPL->appendJsFile('personalization');
		$productData = Db_Products_Products::getProductDataForPersonalizationByProductId(self::$PAGE['pp_pagination_number']);
		$defaultDeliveryMethod = Db_Locale_ProjectCountriesDeliveryMethods::findByFields(array(
			'pcdm_project_id' => self::$PROJECT['id'],
			'pcdm_is_default' => true
		));
		if($defaultDeliveryMethod){
			self::$TPL->assign('defaultDeliveryMethod', $defaultDeliveryMethod);
		}
        $productId = self::$PAGE['pp_pagination_number'];
        $productRow = Db_Products_Products::findById($productId);
        $productName = $productRow->p_name;
        $productDescription = $productRow->p_description;
        $contactPhoneNumber1 = self::$SETTINGS['contact_phone_number1'];
        self::$TPL->assign('productName', $productName);
        self::$TPL->assign('productDescription', $productDescription);
        self::$TPL->assign('contactPhoneNumber1', $contactPhoneNumber1);
		$productMemoryCartIds = self::$S->Get('cart');
		$memoryListProductId = (get('id', 'str') ? get('id', 'str') : null);
		if(isset($productMemoryCartIds[$memoryListProductId]) && $memoryListProductPrice = $productMemoryCartIds[$memoryListProductId]) {
			self::$TPL->assign('memoryProductPrice', $memoryListProductPrice);
		}

		if ($productData) {
			$prices = Db_Products_Prices::getPricesInfo($productData['prc_category_id'], $productData['p_size_id']);
			self::$TPL->assign('prices', $prices);
		}
		return array(
			'type' => 'display',
		);

	}

	public function saveInCartAction() {
		$arrayCart = self::$S->Get('cart');
		//verify if priceId exist in db
		$productId = Utils::memoryProductIdDecode(post('productId', 'str'));
		$memoryListProductIdEncoded = post('memoryProductId', 'str');
		$memoryListProductId = Utils::memoryProductIdDecode($memoryListProductIdEncoded);
		$productData = Db_Products_Products::getProductDataForPersonalizationByProductId($productId);
		$priceInfo = Db_Products_Prices::getAmountForPrice(post('priceId', 'int'), self::$PROJECT['id'], $productData['prc_category_id'], $productData['p_size_id']);
		$memoryListProduct = new Db_Products_MemoryListProducts($memoryListProductId);
		$amount = post('amount', 'int');

		$data = array(
			'imgUrl' => self::$PROJECT['pr_resources_' . conf('ENVIRONMENT')] . $productData['ph_path'] . $productData['ph_resized'],
			'titleProduct' => t(T_PRODUCTS_TITLE, $productData['p_name']),
			'mlp_name' => $memoryListProduct->objectArray->mlp_name
		);

		$arrayCart[$memoryListProductIdEncoded] = array(
			'productId' => post('productId', 'str'),
			'categoryId' => $productData['prc_category_id'],
			'themeId' => post('themeId', 'int'),
			'priceId' => $priceInfo['priceId'],
			'amount' => ($amount) ? $amount : $priceInfo['am_amount']
		);

		self::$S->Set('cart', $arrayCart);
		return array(
			'type' => 'json',
			'content' => $data
		);
	}
}